package com.welcome_2.beta;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.welcome_2.android.R;


public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
	String PHONE;
	String MAC;
	/**Called when activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Method that will save username to SQL database
        getMACAddress();
        getPhoneNumber();
        loadPreferences();
        
	}


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected (MenuItem item){
    	super.onOptionsItemSelected(item);
    	
    	switch(item.getItemId()){
    	case R.id.preferences:
    	onClick();
    	break;
    	}
    	return true;
    }
    
    public void onClick(){
    	Intent intent = new Intent(MainActivity.this, Preferences.class);
    	startActivity(intent);
    
    }
    /**
    public void onClick(){
    	AlertDialog ad = new AlertDialog.Builder(this)
    	.setMessage("terms and conditions")
    	.setIcon(R.drawable.ic_action_edit)
    	.setPositiveButton("Yes", (OnClickListener) this)
    	.setNegativeButton("No", (OnClickListener) this)
    	.setNeutralButton("Cancel" , (OnClickListener) this)
    	.setCancelable(true)
    	.create();
    	ad.show();
    }
  **/
    public void loadPreferences(){
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Preferences prefs= new Preferences();
    
    TextView usernameText = (TextView)findViewById(R.id.usernameTxt);
    usernameText.setText("Welcome, "+ settings.getString("username","") + "!");  
    
    TextView phoneText = (TextView)findViewById(R.id.phoneTxt);
    phoneText.setText("Phone Number:" + "\n" + PHONE);
    settings.registerOnSharedPreferenceChangeListener(MainActivity.this);
    
    TextView macText = (TextView)findViewById(R.id.macTxt);
    macText.setText("MAC Address:" + "\n" + MAC);
    settings.registerOnSharedPreferenceChangeListener(MainActivity.this);
    
    
    
    }


	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		//this will reload preferences
		loadPreferences();
		
	}
	
	public void getMACAddress(){
		WifiManager wifiMan = (WifiManager) this.getSystemService(
                Context.WIFI_SERVICE);
		
		if(wifiMan.isWifiEnabled()){
		//Turn Wi-Fi on
		//wifiMan.setWifiEnabled(true);
		WifiInfo wifiInf = wifiMan.getConnectionInfo();
		MAC = wifiInf.getMacAddress();
		}
		else{
			//Turn Wi-Fi on
			wifiMan.setWifiEnabled(true);
			WifiInfo wifiInf = wifiMan.getConnectionInfo();
			MAC = wifiInf.getMacAddress();
			//Turn Wi-Fi off
			wifiMan.setWifiEnabled(false);
		}
	}
	
	public void getPhoneNumber(){
		TelephonyManager telephony = (TelephonyManager)
				getSystemService(TELEPHONY_SERVICE);
		
		PHONE = telephony.getLine1Number();
	}
}
